
#Set up

#s1
rm(list=ls())

setwd ("C:/Users/cecidy/Google Drive/ZH_analysis/")

source("ZHfunctions.R")
options(scipen=9999)

ZeroHunger <- read.csv("Data/ZHdf/ZeroHungerDF_2020.csv",header=TRUE, sep=",")

#s2
######################################################################################################################

#start1_

#PRONAF and Infant mortality 00 to 10 core sample

#######################################################################################################################

#Make a nonBF ZHvariable!

summary(ZeroHunger$PRONAFPerCapTotal00to10for2000Analysis1000)
summary(ZeroHunger$ZHPerCapTotal00to10for2000Analysis1000)

ZeroHunger$ZHPerCapTotal00to10for2000Analysis1000NOPRONAF <- with(ZeroHunger,ZHPerCapTotal00to10for2000Analysis1000-PRONAFPerCapTotal00to10for2000Analysis1000)

#######################################################################################################################

#1. Prepare each dataframe

#Get Rural municipalities
Poverty04 <- subset(ZeroHunger, PopDensity20002000Analysis<=150)

#Get list of vars
load(file = "Data/ForSamples/MPI00list.rda")
load(file = "Data/ForSamples/Basic00.rda")

RuralPoverty04 <- subset(Poverty04, select=c("PRONAFPerCapTotal00to10for2000Analysis1000","ZHPerCapTotal00to10for2000Analysis1000NOPRONAF",Basic00,MPI00List,
                                             "CensusInfMort2000for2000Analysis100","CensusInfMor2010for2000Analysis100"))
names(RuralPoverty04)
head(RuralPoverty04)

RuralPoverty04 <- na.omit(RuralPoverty04)

#Then take away the ones affected by rural credit
load(file = "Data/ForSamples/EcludeForRCProb00.rda")

RuralPoverty04 <- subset(RuralPoverty04,(!(IBGECode7digit %in% EcludeForRCProb00)))
4863-4846

#Just copy to get the "quality" dataset
RuralPoverty04quality <- RuralPoverty04

#############################################################################################

#Make logged variables

#First make MPI with the correct 0s
test <- NrOfZeroALL(RuralPoverty04quality)#This gives me all the vars with 0s
MPIadd <- test[[1]]
Otheradd <- test[[2]]

#For the MPI because Ive not previously renamed when adding I just override the vars with 0 with the minimum/2
WithAdded <- lapply(MPIadd,function(y) AddConstant(RuralPoverty04quality,y,addName=""))
WithAdded <- data.frame(do.call("cbind",WithAdded))

RuralPoverty04quality[,MPIadd] <- WithAdded[,MPIadd]#just override

#Then I need to make the MPI - here I need to use the 00 function
RuralPoverty04quality <- MakeMPI00(RuralPoverty04quality)

#######################################################################################################################

#Then for the others I cbind them on, and because Ive not included any of previous noCero there no duplicates
RuralPoverty04quality <- cbind(RuralPoverty04quality,lapply(Otheradd,function(y) AddConstant(RuralPoverty04quality,y,addName="nocero")))#

###########################################################################################################################

#And finally I need to log variables - get a list of all names

OtheraddCero <- paste(Otheradd,"nocero",sep="")#here which are names no cero

#All these need log
cols.log <- c("ZHPerCapTotal00to10for2000Analysis1000NOPRONAF","MPI2000for2000Analysis","MPI2010for2000Analysis",
              "GDPPerCapPublicrealN2000for2000Analysis1000",OtheraddCero,"RemoteMinPopFifty2000Analysis",
              "CensusInfMort2000for2000Analysis100","CensusInfMor2010for2000Analysis100",
              "PopDensity20002000Analysis","SizeKm22000Analysis","MeanSlopefor2000analysis","MeanElevationfor2000analysis")
addlog <- function(x) log10(x)
Log10variables <- data.frame(sapply(RuralPoverty04quality[cols.log],addlog))
colnames(Log10variables) <- paste(colnames(Log10variables), "log10",sep="")

#Get all onto ZH
RuralPoverty04quality <- cbind(RuralPoverty04quality,Log10variables)

#but for PRONAF I dont want to have the nocero there
colnames(RuralPoverty04quality)[colnames(RuralPoverty04quality)=="PRONAFPerCapTotal00to10for2000Analysis1000nocerolog10"] <- "PRONAFPerCapTotal00to10for2000Analysis1000log10"

###########################################################################################################################

#Take away the redundandt
Log10variables <- NULL
Poverty04 <- NULL
WithAdded <- NULL
ZeroHunger <- NULL

#############################################################################################

#I make it on log here but that I might have to go back and change after checking for log or not!
RuralPoverty04quality<- getDataFrame(RuralPoverty04quality,"contr.Sum",
                                     contrastVar1="stateName",contrastVar2="Biomefor2000Analysis",setRefToMostCommonLevel,
                                     BaseVar="PRONAFPerCapTotal00to10for2000Analysis1000log10",
                                     MainName="PRONAFPerCapTotal00to10for2000Analysis1000log10scaledMain")

#fin1_
###############################################################################################################

#start2_

#Prepare vectors of variables for regressions

###############################################################################################################

Depvar <- "CensusInfMor2010for2000Analysis100"
DepVarLog <- "CensusInfMor2010for2000Analysis100"

IndepVarLin <- "PRONAFPerCapTotal00to10for2000Analysis1000log10scaledMain"#already scaled so dont need to do it again

baselineDepVar <- "scale(CensusInfMort2000for2000Analysis100)"

stateName <- "stateName"

intLin <- c("PRONAFPerCapTotal00to10for2000Analysis1000log10scaledMain","stateName")
intLin <- paste(intLin,collapse="*")

xvars <- c("scale(MPI2000for2000Analysis)",
           "scale(GDPPerCapPublicrealN2000for2000Analysis1000log10)",
           "scale(TotalKcal2000percapperdaynocerolog10)",
           "scale(TotalHectareCrops2000for2000Analysisnocerolog10)","scale(TotalHectarePasture2006for2000Analysisnocerolog10)",
           "scale(TotalHectareFarmsLess502006for2000Analysisnocerolog10)","scale(RemoteMinPopFifty2000Analysislog10)",
           "scale(ChangeCummulativeSPEI98and00to08and10for2000Analysis)",
           "scale(TotRCpcNOPRONAF00to10for2000Analysis1000nocerolog10)",
           "scale(MeanElevationfor2000analysislog10)","scale(MeanSlopefor2000analysislog10)",
           "scale(PopDensity20002000Analysislog10)","scale(SizeKm22000Analysislog10)","scale(ZHPerCapTotal00to10for2000Analysis1000NOPRONAFlog10)",
           "Biomefor2000Analysis")

#For CBPS models
cbpsDepVar <- "PRONAFPerCapTotal00to10for2000Analysis1000log10"
cbpsbaselineDepVar <- "CensusInfMort2000for2000Analysis100"

cbpsVars <- c("MPI2000for2000Analysis",
              "GDPPerCapPublicrealN2000for2000Analysis1000log10","TotalKcal2000percapperdaynocerolog10",
              "TotalHectareCrops2000for2000Analysisnocerolog10","TotalHectarePasture2006for2000Analysisnocerolog10",
              "TotalHectareFarmsLess502006for2000Analysisnocerolog10","RemoteMinPopFifty2000Analysislog10",
              "ChangeCummulativeSPEI98and00to08and10for2000Analysis",
              "TotRCpcNOPRONAF00to10for2000Analysis1000nocerolog10",
              "MeanElevationfor2000analysislog10","MeanSlopefor2000analysislog10",
              "PopDensity20002000Analysislog10","SizeKm22000Analysislog10","ZHPerCapTotal00to10for2000Analysis1000NOPRONAFlog10",
              "stateName","Biomefor2000Analysis")

#########################################################################################################################

#fin2_

#temp1
#temp2

################################################################################################################################

